package com.banciyuan.bcywebview.base.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.common.UIUtils;


/**
 * Created by squall on 2016/5/25.
 */
public class ColorLabel extends View {

    private CharSequence labeltext;

    private float text_size;
    private int back_color;
    private int extra_width;


    public ColorLabel(Context context) {
        super(context);
    }

    public ColorLabel(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttr(context, attrs);
    }

    public ColorLabel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttr(context, attrs);
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ColorLabel(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        getAttr(context, attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(text_size);
        textPaint.setColor(Color.WHITE);

        Paint backPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backPaint.setColor(back_color);
        backPaint.setStyle(Paint.Style.FILL);
        backPaint.setStrokeWidth(30.0f);

        int height_text = 0;
        int width_text = 0;
        if (labeltext != null) {
            height_text = (int) text_size;
            width_text = getTextWidth(labeltext.toString(), textPaint);
        }

        int height = height_text + getPaddingTop() + getPaddingBottom();
        int width = width_text + getPaddingLeft() + getPaddingRight();
        float baseline = (height - textPaint.getFontMetrics().bottom - textPaint.getFontMetrics().top) / 2;

        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(width + extra_width, 0);
        path.lineTo(width, height);
        path.lineTo(0, height);
        path.close();

        canvas.drawPath(path, backPaint);
        if (labeltext != null) {
            canvas.drawText(labeltext.toString(), getPaddingLeft(), baseline, textPaint);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(text_size);
        int height_text = 0;
        int width_text = 0;
        if (labeltext != null) {
            height_text = (int) text_size;
            width_text = getTextWidth(labeltext.toString(), textPaint);
        }
        int height = height_text + getPaddingTop() + getPaddingBottom();
        int width = width_text + getPaddingLeft() + getPaddingRight() + extra_width;

        setMeasuredDimension(width, height);
    }

    public void setText_size(int size) {
        this.text_size = size;
        postInvalidate();
    }

    public void setBackGround(int color) {
        this.back_color = color;
        postInvalidate();
    }

    public void setExtra_width(int size) {
        this.extra_width = size;
    }

    public void setText(String text) {
        this.labeltext = text;
        postInvalidate();
    }


    private int getTextWidth(String word, Paint textPaint) {
        if (word != null) {
            float text_width = textPaint.measureText(word);
            int boarder_width = (int) (text_width);
            return boarder_width;
        } else {
            return 0;
        }
    }

    private void getAttr(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ColorLabel);
        text_size = a.getDimension(R.styleable.ColorLabel_textsize, UIUtils.dip2px(12, getContext()));
        back_color = a.getColor(R.styleable.ColorLabel_backgroundColor, getResources().getColor(R.color.red_label));
        extra_width = (int) a.getDimension(R.styleable.ColorLabel_extrawidth, UIUtils.dip2px(6, getContext()));
        labeltext = a.getText(R.styleable.ColorLabel_labeltext);

    }
}